home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Shift Layout ƒ / Shift Layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-18  |  13.0 KB  |  419 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Shift Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
  11. |**|    "LayoutLibrary.c", "ShapeLibrary.c", and "TransformLibrary.c".
  12. |**|
  13. |**|    6/96 bob    Updated #includes to support changed GX Library names.
  14. |**|                Updated the copyright date.
  15. |**|
  16. |**|    ©1990 - 1996  Apple Computer, Inc.
  17. |**|    All rights reserved.
  18. |**|
  19. |**| =====================================================================
  20. \**/
  21.  
  22.  
  23. #include "QDGX shell.h"
  24. #include <GXLayout.h>
  25. #include "LayoutLibrary.h"
  26.  
  27.  
  28. /**\
  29. |**| ---------------------------------------------------------------------
  30. |**| PROTOTYPES
  31. |**| ---------------------------------------------------------------------
  32. \**/
  33.  
  34. // funtions required by shell
  35.  
  36. void    DoSetup            (void);
  37. void    DoDraw            (WindowPtr wind, Boolean updating);
  38. OSErr    DoCreateNew        (void);
  39. void    DoDispose        (WindowPtr wind);
  40. void    DoIdle            (WindowPtr wind);
  41. void    DoTeardown        (void);
  42. void    DoClick            (WindowPtr wind, Point p);
  43.  
  44. // private functions
  45.  
  46. OSErr    DoWindowInit        (WindowPtr wind);
  47. void    CreateSampleImage    (WindowPtr wind);
  48.  
  49.  
  50. /**\
  51. |**| ---------------------------------------------------------------------
  52. |**| ENUMS
  53. |**| ---------------------------------------------------------------------
  54. \**/
  55. enum { rWindResource = 128 };
  56.  
  57.  
  58. /**\
  59. |**| ---------------------------------------------------------------------
  60. |**| GLOBALS
  61. |**| ---------------------------------------------------------------------
  62. \**/
  63. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  64. // functionality will only work with the "debugging" version of QuickDraw GX.
  65. // If the debugging version is not installed, nothing bad will happen, but these
  66. // functions will not work. 
  67.  
  68. Boolean        gDebugging = true;
  69.  
  70. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  71.  
  72. Boolean        gGiveMeValidation = true;
  73.  
  74.  
  75. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  76. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  77. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  78. // since we need abunch if we have several windows open.
  79.  
  80. long        gGraphicsHeapSize = 600;
  81.  
  82. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  83. // override.  This is so that our override can be native PowerPC code if necessary.
  84.  
  85. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  86.  
  87.  
  88.  
  89. /**\
  90. |**| ---------------------------------------------------------------------
  91. |**| DoSetup()
  92. |**| Here's where we initialize any global variables our application needs.
  93. |**| We have only one at this time -- the universal proc pointer for
  94. |**| our printing override.
  95. |**| ---------------------------------------------------------------------
  96. \**/
  97. void DoSetup (void)
  98. {    // Initialize our printing event override UPP
  99.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  100. }
  101.  
  102.  
  103. /**\
  104. |**| ---------------------------------------------------------------------
  105. |**| DoDraw()
  106. |**| Draw the contents of the window.  The first parameter is the window
  107. |**| to draw, and the second parameter is true if we're updating an existing
  108. |**| image.  If that's the case, we don't want to change anything, but
  109. |**| just draw what's already there.
  110. |**| ---------------------------------------------------------------------
  111. \**/
  112. void DoDraw (WindowPtr wind, Boolean updating)
  113. {
  114.      #pragma unused (updating)
  115.      GXDrawShape (GetDocShape(wind));
  116. }
  117.  
  118.  
  119. /**\
  120. |**| ---------------------------------------------------------------------
  121. |**| DoCreateNew()
  122. |**| This routine is called when a window needs to be created.
  123. |**| ---------------------------------------------------------------------
  124. \**/
  125. OSErr DoCreateNew (void)
  126. {
  127.     OSErr        err = noErr;
  128.     WindowPtr    wind;
  129.     
  130. // Get and create our window from the resource fork
  131.  
  132.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  133.  
  134. // Attach a default gxViewPort to it, create and iInitialize our
  135. // private data for it, and add a sample image to its page shape.
  136.  
  137.     if ( wind == NULL )
  138.         return (MemError());
  139.  
  140.     GXIgnoreGraphicsNotice(transform_already_set);
  141.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  142.     GXPopGraphicsNotice();
  143.     
  144.     err = DoWindowInit(wind);
  145.     if ( err != noErr )
  146.         return err;
  147.     
  148.     CreateSampleImage(wind);
  149.     return err;
  150. }
  151.  
  152.  
  153. /**\
  154. |**| ---------------------------------------------------------------------
  155. |**| DoDispose()
  156. |**| This routine is called when a window needs to be disposed of.
  157. |**| ---------------------------------------------------------------------
  158. \**/
  159. void DoDispose (WindowPtr wind)
  160. {
  161.     TH_Doc    doc;
  162.     
  163. // You should always dispose of your GX graphics objects before tossing your window.
  164. // Why?  It's generally good form and this approach guarantees that everything is
  165. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  166. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  167. // with notices set, you will receive a notice that you had not disposed of everything.
  168. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  169.     
  170.     if ( wind != NULL )
  171.     {
  172.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  173.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  174.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  175.         DisposHandle((Handle) doc);            // Dispose of our private data.
  176.         DisposeWindow(wind);                // Dispose of the window.
  177.     }
  178. }
  179.  
  180.  
  181. /**\
  182. |**| ---------------------------------------------------------------------
  183. |**| DoIdle()
  184. |**| This routine is called to do things while idling through the event loop.
  185. |**| ---------------------------------------------------------------------
  186. \**/
  187. void DoIdle (WindowPtr wind)
  188. {
  189. }
  190.  
  191.  
  192. /**\
  193. |**| ---------------------------------------------------------------------
  194. |**| DoTeardown()
  195. |**| This routine is called just before we quit to remove anything 
  196. |**| persistent that might have been setup by DoSetup().
  197. |**| ---------------------------------------------------------------------
  198. \**/
  199. void DoTeardown (void)
  200. {
  201.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  202. }
  203.  
  204.  
  205. /**\
  206. |**| ---------------------------------------------------------------------
  207. |**| DoClick()
  208. |**| ---------------------------------------------------------------------
  209. \**/
  210. void DoClick(WindowPtr window, Point p)
  211. {
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218. /**\
  219. |**| ---------------------------------------------------------------------
  220. |**| DoWindowInit()
  221. |**| In this function we create and initialize the the private document
  222. |**| structure for a new window.  This structure contains the print job and
  223. |**| the shape which is drawn in the window.  We store this data in a handle
  224. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  225. |**| this, rather than using globals, we can create many windows containing
  226. |**| unique print jobs and shapes.
  227. |**| ---------------------------------------------------------------------
  228. \**/
  229. OSErr DoWindowInit (WindowPtr wind)
  230. {
  231.     OSErr    err = noErr;
  232.     gxJob    docJob;
  233.     gxShape    docPage;
  234.     TH_Doc    windDoc;
  235.  
  236.  
  237. // Create the page shape. We set the unique items attribute to make sure that each item
  238. // added to the picture has a unique reference. If this attribute was not set, we would
  239. // not see all copies of anything we add to the shape multiple times -- we'd just see
  240. // the last version added.        
  241.  
  242.     docPage = GXNewShape(gxPictureType);
  243.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  244.     
  245.     
  246. // Create a print job for this document.  This will be the same as the system default until
  247. // the user goes through the dialogs for Page Setup or Print…
  248.  
  249.     err = GXNewJob(&docJob);
  250.     
  251.     
  252. // If there are no errors, create a handle the size of our document structure and store
  253. // the print job and page shape in it.  Store the handle in the window's refCon field so
  254. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  255. // can be used to do this easily.
  256.  
  257.     if ( err == noErr )
  258.     {
  259.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  260.  
  261.         if ( windDoc == NULL )
  262.             err = MemError();
  263.         else
  264.         {
  265.             (*windDoc)->docJob = docJob;
  266.             (*windDoc)->docPage = docPage;
  267.             SetWRefCon(wind, (long) windDoc);
  268.         }
  269.  
  270. // Now install our application override for PrintingEvent so that we can
  271. // support the new movable-modal printing dialog boxes.
  272.  
  273.         GXInstallApplicationOverride(docJob, gxPrintingEventMsg, gOurPrintingOverrideUPP);
  274.  
  275.     }
  276.  
  277.     return err;
  278. }
  279.  
  280.  
  281. /**\
  282. |**| ---------------------------------------------------------------------
  283. |**| CreateSampleImage()
  284. |**| This function creates primitive shapes and adds them to the window's page shape.
  285. |**| ---------------------------------------------------------------------
  286. \**/
  287. void CreateSampleImage (WindowPtr wind)
  288. {
  289.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  290.                                             // in QuickDraw coordinates
  291.     gxShape layout;                        // the layout shape we build
  292.     gxShape thePage;                    // this window's document shape
  293.     gxRunControls runControls;            // run controls for the layout shape
  294.     gxLayoutOptions layoutOptions;        // options for the layout shape
  295.     gxStyle textStyles[5];                // an array of text style for our text runs
  296.     
  297. // These are the five text runs for this layout shape, in pieces because they're
  298. // in different languages
  299.     
  300.     char *text1 = "Office";
  301.     
  302. // The following is "Macintosh" in Arabic: 
  303. // meem, alif, kaf,  noon, tah,  wau,  shin
  304. // (This assumes the standard Arabic character set for Macintosh, by the way)
  305.     
  306.     char *text2 = "AWAY";
  307.     
  308.     char *textRuns[2];                    // an array of pointers to text runs
  309.  
  310.     short textLengths[2];                // an array of the lengths of each run
  311.     short totalLength;                    // the total length of the layout's text
  312.     short level0 = 0;                    // variable to take address of later
  313.     
  314.     gxPoint posn;                        // the position of the layout shape
  315.  
  316.  
  317. // First, initialize the textRuns array to point to the two text runs
  318.     
  319.     textRuns[0] = text1;
  320.     textRuns[1] = text2;
  321.     
  322. // Next, initialize the textLengths and levelRunLengths arrays.  MyStrLength() is
  323. // in this file.
  324.  
  325.     textLengths[0] = MyStrLength (text1);
  326.     textLengths[1] = MyStrLength (text2);
  327.     
  328. // totalLength is the length of all the text.
  329.  
  330.     totalLength = textLengths[0] + textLengths[1];
  331.     
  332.  
  333. // make default gxLayoutOptions and gxRunControls structures
  334.  
  335.     InitializeLayoutOptions (&layoutOptions);
  336.     InitializeRunControls (&runControls);
  337.     
  338.     
  339. // Position the layout half way down the left edge of the window. Set 
  340. // the layout's width to the window's width and set the flushness to 1/2, this
  341. // will cause the layout to center in the window.  Note that ourWindowRect is
  342. // not in fixed coordinates, so we have to make it fixed to use it.
  343.  
  344.     layoutOptions.width = ff(ourWindowRect.right - ourWindowRect.left);
  345.     layoutOptions.flush = fract1/2;
  346.  
  347.     posn.x = 0;
  348.     posn.y = ff((ourWindowRect.bottom - ourWindowRect.top) / 2);
  349.     
  350.  
  351. // Create the styles we'll use. 
  352. // NewLayoutStyle is a library routine found in layout library.c.  
  353.     
  354.     textStyles[0] = NewLayoutStyle(
  355.         (char *) "\pHoefler Text",            // the gxFontName for the style
  356.         ff(60),                                // the text size in fixed point
  357.         0,                                    // gxTextAttributes
  358.         &runControls,                        // run controls (our default ones)
  359.         nil,                                // run features (none for this style)
  360.         0,                                    // count of run features
  361.         nil);                                // style run overrides (none right now)
  362.  
  363. // Now set the run controls to add 30 points cross-stream kerning and -4 points
  364. // with-stream kerning, which moves the text four points closer together than
  365. // normal kerning would.
  366.  
  367.     runControls.crossStreamShift = ff(30);
  368.     runControls.beforeWithStreamShift = ff(-4);
  369.  
  370. // Now make another style with our new shifted kerning
  371.  
  372.     textStyles[1] = NewLayoutStyle(
  373.         (char *) "\pHoefler Text",            // the gxFontName for the style
  374.         ff(30),                                // the text size in fixed point
  375.         0,                                    // gxTextAttributes
  376.         &runControls,                        // run controls (our default ones)
  377.         nil,                                // run features (none for this style)
  378.         0,                                    // count of run features
  379.         nil);                                // style run overrides (none right now)
  380.     
  381.  
  382. // Setup complete!  Build the layout.
  383.  
  384.     layout = GXNewLayout(
  385.         2,                                // count of text runs
  386.         textLengths,                    // array of lengths of each run
  387.         (void *) textRuns,                // array of pointers to the text
  388.         2,                                // count of style runs
  389.         textLengths,                    // array of the byte lengths of each run
  390.         textStyles,                        // array of the styles
  391.         1,                                // number of levels in the layout
  392.         &totalLength,                    // array of the lengths of each level
  393.         &level0,                        // array of the levels
  394.         &layoutOptions,                    // options (default)
  395.         &posn);                            // position of the layout shape
  396.  
  397.     
  398. // Retrieve the page shape so we can add to it.
  399.  
  400.     thePage = GetDocShape(wind);
  401.     
  402. // Add the layout to the window's picture shape.  AddToShape is in shape library.c.
  403.  
  404.     AddToShape(thePage, layout);
  405.  
  406. // Dispose of what we created
  407.  
  408.     GXDisposeShape(layout);
  409.     GXDisposeStyle(textStyles[0]);
  410.     GXDisposeStyle(textStyles[1]);
  411.     
  412.  
  413. // Invalidate the window's portRect so that everything gets updated.
  414.     
  415.     SetPort(wind);
  416.     InvalRect(&ourWindowRect);
  417. }
  418.  
  419.